package com.yiqi.news.mvp.ui.adapter

import android.os.Bundle
import android.support.transition.Visibility
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jess.arms.utils.ArmsUtils
import com.yiqi.huize.loadImage
import com.yiqi.news.R
import com.yiqi.news.app.utils.TimeUtils
import com.yiqi.news.entity.News
import com.yiqi.news.mvp.ui.activity.NewsDetailActivity
import kotlinx.android.synthetic.main.item_news.view.*

class NewsAdapter : BaseQuickAdapter<News, BaseViewHolder>(R.layout.item_news) {

    override fun convert(helper: BaseViewHolder, item: News) {
        val itemView = helper.itemView


//        itemView.iv_news_delete.visibility = if (helper.adapterPosition == 0 || helper.adapterPosition == 1) GONE else VISIBLE
        itemView.tv_news_title.text = item.title
        itemView.tv_news_label.text = "#" + item.labelName
        itemView.tv_news_read_count.text = item.readCount.toString() + "浏览"
        itemView.tv_news_read_time.text = TimeUtils.getShortTime(item.behotTime * 1000)
        itemView.iv_news_delete.setOnClickListener { remove(helper.adapterPosition) }

        if (!item.image.isNullOrEmpty()) {
            itemView.iv_news_thumb.loadImage(item.image)
        } else {
            itemView.iv_news_thumb.visibility = if (item.image.isNullOrEmpty()) GONE else VISIBLE
        }

        itemView.setOnClickListener {
            var bundle = Bundle()
            bundle.putString("news_id", item.id)
            ArmsUtils.startActivity(mContext, NewsDetailActivity::class.java, bundle)
        }
    }
}
